package capricornus.utils.blood
{
	import capricornus.core.ClassManager;
	
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	/**
	 * 血条，也可以用做魔法条
	 * @author kael
	 * 
	 */	
	public class Blood extends Sprite
	{
		private var _red:Bitmap;
		private var _redMask:Shape;
		private var _orange:Bitmap;
		private var _orangeMask:Shape;
		private var _back:Bitmap;
		
		private var _proportion:Number = 1;
		
		/**
		 * 
		 * @param redClassName 红色条资源名称；不可以为空
		 * @param orangeClassName 浅色条资源名称；可以为空
		 * @param backClassName 背景资源名称；可以为空
		 * 
		 */		
		public function Blood( redClassName:String, orangeClassName:String, backClassName:String )
		{
			super();
			init( redClassName, orangeClassName, backClassName );
		}
		
		private function init( redClassName:String, orangeClassName:String, backClassName:String ):void
		{
			RedBloodPanel;
			OrangeBloodPanel;
			BackBloodPanel;
			_red = new Bitmap();
			_red.bitmapData = ClassManager.getClassInstance( redClassName );
			_redMask = new Shape();
			_redMask.graphics.beginFill(0);
			_redMask.graphics.drawRect( 0, 0, _red.width, _red.height );
			_redMask.graphics.endFill();
			_red.mask = _redMask;
			addChild( _red );
			addChild( _redMask );
			
			var bd:BitmapData;
			bd = ClassManager.getClassInstance( orangeClassName );
			if( bd != null )
			{
				_orange = new Bitmap( bd );
				_orangeMask = new Shape();
				_orangeMask.graphics.beginFill(0);
				_orangeMask.graphics.drawRect( 0, 0, _orange.width, _orange.height );
				_orangeMask.graphics.endFill();
				_orange.mask = _orangeMask;
				addChildAt( _orange, 0 );
				addChild( _orangeMask );
			}
			
			bd = ClassManager.getClassInstance( backClassName );
			if( bd != null )
			{
				_back = new Bitmap(bd);
				addChildAt( _back, 0 );
			}
		}
		
		public function destroy():void
		{
			if( _red )
			{
				_red.bitmapData = null;
				if( _red.parent ) _red.parent.removeChild( _red );
			}
			if( _orange )
			{
				TweenLite.killTweensOf( _orangeMask );
				_orange.bitmapData = null;
				if( _orange.parent ) _orange.parent.removeChild( _orange );
			}
			if( _back )
			{
				_back.bitmapData = null;
				if( _back.parent ) _back.parent.removeChild( _back );
			}
			if( parent ) parent.removeChild( this );
		}
		
		/**
		 * 设置比例 
		 * @param value 比例值；value>1 value=1; value<0 value=0;
		 * 
		 */		
		public function set proportion( value:Number ):void
		{
			if( value < 0 ) value = 0;
			if( value > 1 ) value = 1;
			if( _proportion != value )
			{
				if( _orangeMask != null )
				{
					if( _proportion > value )
					{
						TweenLite.to( _orangeMask, 1, {scaleX:value} );
					}
					else
					{
						TweenLite.killTweensOf( _orangeMask );
						_orangeMask.scaleX = value;
					}
				}
				_redMask.scaleX = value;
				_proportion = value;
			}
		}
	}
}















